package com.kimho.transit.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.transit.domain.Transit;
import com.kimho.transit.service.TransitService;
import com.kimho.util.Util;
@Controller
@RequestMapping("/transit")
public class TransitController {

	@Autowired
	private TransitService transitService;
	/**
	 * 登录验证
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/items",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getTransitList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		List<Transit> transit = transitService.getTransitList(param);
		responseMap.put("totalCount", transitService.getTotal(param));
		responseMap.put("rows", transit);
		return responseMap;
	}
	
	@RequestMapping(value="/view",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getTransitdetList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		List<Transit> transit = transitService.getTransitdetList(param);
		responseMap.put("totalCount", transit.size());
		responseMap.put("rows", transit);
		return responseMap;
	}

	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String owner = request.getParameter("owner");
		String tell_nbr = request.getParameter("tell_nbr");
		String addr = request.getParameter("addr");
		String local_id= request.getParameter("local_id");
		
		
		try {
			//编辑用户信息
			if(!"".equals(id)) {
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("id", id);

				responseMap.put("success", "true");
				responseMap.put("info", "编辑成功！");
			}
			//新增用户信息
			else {
				Transit transit = new Transit();
				String newid = Util.getUUID();
			    
				transit.setTransit_id(newid);
				transit.setName(name);
				transit.setOwner(owner);
				transit.setTell_nbr(tell_nbr);
				transit.setLocal_id(local_id);
				transit.setAddr(addr);
			    
				Map<String,Object> param = new HashMap<String,Object>();
				transitService.insertTransit(transit, param);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> remove(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String ids = request.getParameter("ids");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("ids", ids);
		try {
			transitService.deleteTransit(param);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}


}
